package edu.spec.sms.controller;


import edu.spec.sms.model.Clazz;
import edu.spec.sms.model.Student;
import edu.spec.sms.service.iface.IClazzService;
import edu.spec.sms.service.iface.IStudentService;
import edu.spec.sys.model.User;
import edu.spec.sys.model.dto.ExtGridReturn;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.enumeration.Status;
import org.madgecko.basic.model.Pager;
import org.madgecko.basic.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/student")
public class StudentController {
	@Inject
	private IStudentService studentService;
	@Inject
	private IClazzService clazzService;

	@ResponseBody
	@RequestMapping("/add")
	public Object add(Student student , Integer clazzId, HttpSession session){
		Clazz clazz = clazzService.load(clazzId);
		student.setBjid(clazz.getId());
		student.setBjmc(clazz.getName());
		student.setCreateDate(new Date());
		User u = (User)session.getAttribute("user");
		studentService.add(student);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/delete/{id}")
	public Object delete(@PathVariable Integer id){
		studentService.delete(id);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/update")
	public Object update(Student s, Integer clazzId){
		Student dbs = studentService.load(s.getId());
		dbs.setStatus(s.getStatus());
		dbs.setName(s.getName());
		dbs.setXh(s.getXh());
		dbs.setSfzh(s.getSfzh());
		dbs.setDes(s.getDes());
		dbs.setUpdateDate(new Date());
		Clazz clazz = clazzService.load(clazzId);
		dbs.setBjid(clazz.getId());
		dbs.setBjmc(clazz.getName());
		dbs.setXh(s.getXh());
		dbs.setDes(s.getDes());
		studentService.update(dbs);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/resetPassword/{id}")
	public Object resetPassword(@PathVariable Integer id){
		Student dbs = studentService.load(id);
//		dbs.setPassword(SecurityUtil.md5("123"));
		studentService.update(dbs);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/toggleStatus/{id}")
	public Object toggleStatus(@PathVariable Integer id){
		Student dbs = studentService.load(id);
		if(dbs.getStatus()== Status.DISABLED){
			dbs.setStatus(Status.ENABLED);
		}else if(dbs.getStatus()==Status.ENABLED){
			dbs.setStatus(Status.DISABLED);
		}
		studentService.update(dbs);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/findByCon")
	public Object findStudentByCon(Integer status, Integer clazzId, String con){
		Pager<Student> ps =  studentService.findStudentByCon(status, clazzId, con);
		return new ExtGridReturn(ps.getTotal(), ps.getDatas());
	}

	@ResponseBody
	@RequestMapping("/listStudentByClazz")
	public Object listStudentByClazz(Integer clazzId){
		List<Student> ps = studentService.listStudentByClazz(clazzId);
		return new ExtGridReturn((long)ps.size(), ps);
	}

	@RequestMapping(value="/importIn", method= RequestMethod.POST)
	public void importIn(MultipartFile file, HttpServletRequest req, HttpServletResponse resp, HttpSession session){
		try {
			boolean b = studentService.importIn(file, req, session);
			MvcReturn er = new MvcReturn(b);
			resp.setContentType("text/html;charset=UTF-8");
			resp.getWriter().write(JsonUtil.getInstance().obj2json(er));
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}


}
